package com.mjk.common.ai.work.chat;


import com.mjk.common.ai.base.ThreadContext;
import com.mjk.common.ai.dto.ConfigResp;
import com.mjk.common.ai.dto.ModifyPasswordReq;
import com.mjk.common.ai.dto.UserUpdateReq;
import com.mjk.common.ai.entity.User;
import com.mjk.common.ai.exception.BaseException;
import com.mjk.common.ai.service.UserService;
import com.talanlabs.avatargenerator.Avatar;
import com.talanlabs.avatargenerator.cat.CatAvatar;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

import static com.mjk.common.ai.enums.ErrorEnum.B_IMAGE_LOAD_ERROR;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/{uuid}")
    public void info(@Validated @PathVariable String uuid) {
        log.info(uuid);
    }


    @GetMapping("/config")
    public ConfigResp configInfo() {
        return userService.getConfig();
    }


    @PostMapping("/edit")
    public void update(@Validated UserUpdateReq userUpdateReq) {
        userService.updateConfig(userUpdateReq);
    }


    @PostMapping("/password/modify")
    public String modifyPassword(@RequestBody ModifyPasswordReq modifyPasswordReq) {
        userService.modifyPassword(modifyPasswordReq.getOldPassword(), modifyPasswordReq.getNewPassword());
        return "修改成功";
    }


    @PostMapping("/logout")
    public void logout() {
        userService.logout();
    }


    @GetMapping(value = "/myAvatar", produces = MediaType.IMAGE_JPEG_VALUE)
    public void myAvatar(HttpServletResponse response) {
        User user = ThreadContext.getCurrentUser();
        Avatar avatar = CatAvatar.newAvatarBuilder().build();
        BufferedImage bufferedImage = avatar.create(user.getId());
        //把图片写给浏览器
        try {
            ImageIO.write(bufferedImage, "png", response.getOutputStream());
        } catch (IOException e) {
            log.error("load my avatar error", e);
            throw new BaseException(B_IMAGE_LOAD_ERROR);
        }
    }

    @GetMapping(value = "/avatar/{uuid}", produces = MediaType.IMAGE_JPEG_VALUE)
    public void avatar(@Validated @PathVariable String uuid, @RequestParam(defaultValue = "64") @Min(32) @Max(128) Integer width, @RequestParam(defaultValue = "64") @Min(32) @Max(128) Integer height, HttpServletResponse response) {
        User user = userService.getByUuid(uuid);
        long userId = 0;
        if (null != user) {
            userId = user.getId();
        }
        try {
            writeToResponse(userId, width, height, response);
        } catch (IOException e) {
            log.error("load avatar error", e);
            throw new BaseException(B_IMAGE_LOAD_ERROR);
        }
    }

    private synchronized void writeToResponse(Long userId, Integer width, Integer height, HttpServletResponse response) throws IOException {
        response.setHeader("Cache-Control", "max-age=" + 3600 * 24 * 365);
        Avatar avatar = CatAvatar.newAvatarBuilder().size(width, height).build();
        BufferedImage bufferedImage = avatar.create(userId);
        ImageIO.write(bufferedImage, "png", response.getOutputStream());
    }
}
